package com.hsmw.api.service;

import cn.hutool.json.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hsmw.api.exception.ApiException;
import com.hsmw.api.mapper.HsmwMenuMapper;
import com.hsmw.api.mapper.HsmwRoleMapper;
import com.htn.common.core.constant.CommonConstants;
import com.hsmw.domain.vo.SaveRoleMenuVO;
import com.hsmw.common.base.BaseService;
import com.htn.common.core.base.DataResponse;
import com.hsmw.domain.HsmwMenu;
import com.hsmw.domain.HsmwRole;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author hongfei
 */
@Service
@Slf4j
@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class HsmwRoleService extends BaseService<HsmwRoleMapper, HsmwRole> {

	@Autowired
	private HsmwRoleMapper hsmwRoleMapper;
	@Autowired
	private HsmwMenuMapper hsmwMenuMapper;

	/**
	 * list分页
	 *
	 * @param page
	 * @param hsmwRole
	 * @return
	 */
	public IPage<HsmwRole> getHsmwRolePage(IPage page, HsmwRole hsmwRole) {
		LambdaQueryWrapper<HsmwRole> queryWrapper = new LambdaQueryWrapper<>();
		IPage ipage = this.baseMapper.selectPage(page, queryWrapper.orderByDesc(HsmwRole::getCreateTime));
		return ipage;
	}

	/**
	 * 查询菜单树
	 *
	 * @return
	 */
	public List<HsmwRole> getHsmwRoleSelect() {
		LambdaQueryWrapper<HsmwRole> queryWrapper = new LambdaQueryWrapper<>();
		List<HsmwRole> data = hsmwRoleMapper.selectList(queryWrapper);
		return data;
	}

	/**
	 * 根据ID查询
	 *
	 * @param id
	 * @return
	 */
	public HsmwRole getById(String id) {
		LambdaQueryWrapper<HsmwRole> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(HsmwRole::getId, id);
		return hsmwRoleMapper.selectOne(queryWrapper);
	}

	/**
	 * 新增或修改
	 *
	 * @return
	 */
	public DataResponse saveOrUpdateHsmwRole(HsmwRole hsmwRole) {
		StringBuffer msg = new StringBuffer();
		Integer result = 0;
		if (StringUtils.isNotBlank(hsmwRole.getId())) {
			result = hsmwRoleMapper.updateById(hsmwRole);
		} else {
			HsmwRole existRole = hsmwRoleMapper.selectOne(Wrappers.lambdaQuery(HsmwRole.class).eq(HsmwRole::getName, hsmwRole.getName()));
			if (existRole != null) {
				return new DataResponse(CommonConstants.ResponseStatus.DRIVER_IS_EXIST,CommonConstants.ResponseStatus.ROLE_IS_EXIST.getMessage());
			}
			hsmwRole.setType(CommonConstants.UserRole.BIZ.name());
			result = hsmwRoleMapper.insert(hsmwRole);
		}
		return new DataResponse(result);
	}

	public DataResponse saveRoMenuTree(SaveRoleMenuVO saveRoleMenuVo) {
		HsmwRole hsmwRole = hsmwRoleMapper.selectById(saveRoleMenuVo.getRoleId());
		List<HsmwMenu> hsmwMenus = hsmwMenuMapper.selectBatchIds(saveRoleMenuVo.getMenuIds());
		JSONArray jsonArray = new JSONArray();
		hsmwMenus.forEach(hsmwMenu -> {
			JSONObject metaObject = new JSONObject(hsmwMenu.getMeta());
			hsmwMenu.setMeta(metaObject);
			jsonArray.add(hsmwMenu);
		});
		hsmwRole.setMenuTree(jsonArray.toJSONString(0));
		hsmwRoleMapper.updateById(hsmwRole);
		return new DataResponse();
	}

	public DataResponse deleteRoleById(String id) {

		if(StringUtils.isBlank(id)){
			return new DataResponse(CommonConstants.ResponseStatus.FAIL,"id不可为空");
		}
		return new DataResponse(hsmwRoleMapper.deleteById(id));
	}
}
